#! /bin/sh
##############################################
#
# buiuld script
#
##############################################

####################
# define variableis
####################
date=`date +"%Y%m%d%H%M"`

svn_username=zijing.zhang
svn_password=UPQoDguRhNjrof

p_name=admin
p_home=/home/q/www/"$p_name"
p_root_path="$p_home"/webapps
p_bak_path="$p_home"/bak
p_log_path="$p_home"/logs
p_work_path="$p_home"/work

bakfile="$p_name".bak."$date".tar.gz

tomcat_port=8099

effect_url='
    http://xxxxxx/qos/qos_db.jsp,
    http://xxxxxx/qos/qos_nosql.jsp
'
healthcheck="$p_root_path"/ROOT/healthcheck.html

# extension script
sh_gziplog=ext_gzip_log.sh

svn_username=xxx
svn_password=xxx




##################
# define function
##################
function_get_pid()
{
    pid=`ps -ef|grep java|grep "$p_name"|grep -v "grep"|awk '{print $2}'`
}

function_stop_tomcat()
{
    function_get_pid

    if [ "$pid" ];then
        echo "found running tomcat pid: $pid"

        #
	# Warn: 
        # For a part of application, enforce 'kill -9' may cause unknown problems
        # So to use 'kill -3' instead of it 
	#
        kill -9 "$pid" && sleep 2
        echo "stop tomcat ok!"
    else
        echo "not found running tomcat!" 
    fi
}

function_start_tomcat()
{
    unset pid    

    /home/q/tools/bin/start_tomcat.sh "$p_home" > /dev/null 2>&1

    sleep 10

    function_get_pid

    if [ "$pid" ];then
        echo "start tomcat ok! pid: $pid"
    else
        echo "start tomcat fail!"
    fi
}

function_healthcheck_offline()
{
    rm -f "$healthcheck"

: :||:<<\COMMENTS
    reqcmd="netstat -an|grep ESTABLISHED|awk '{print $4}'|grep $tomcat_port|wc -l"
    reqnum=`eval $reqcmd`

    until [ "$reqnum" -eq "0" ];do
        echo "found request num: $reqnum, sleep 2... "

        sleep 2
        reqnum=`eval $reqcmd`
    done    
COMMENTS

    echo 'healthcheck offine ok!'
}


function_healthcheck_online()
{
    touch "$healthcheck"
    echo 'healthcheck online ok!'
}



##################
# do work
##################
# check svn
svn_version=`svn --version|grep version|head -1|awk '{print $3}'`
[ -z "$svn_version" -o "$?" -ne 0 ] && echo 'svn not found! ' && exit 1  
echo "found svn version: $svn_version"


# check args
[ -z "$1" ] && echo -e "$0 error: input args is null! \n" && exit 1


# import war form svn
svn export "$1" "$p_name".war --username "$svn_username" --password "$svn_password" --no-auth-cache -q
[ "$?" -ne 0 ] && echo -e "$0 error: svn export error! \n" && exit 1
echo  "svn export $1 ok!"


# backup file 
if [ -d "$p_root_path"/ROOT ];then
    tar zcf "$bakfile" -C "$p_root_path" ROOT
    [ "$?" -ne 0 ] && echo -e "$0 error: backup file error! \n" && exit 1

    [ ! -d "$p_bak_path" ] && mkdir -p "$p_bak_path"
    mv -f "$bakfile" "$p_bak_path"
    echo  "backup file ok! bakfile name: $p_bak_path/$bakfile "
fi


# healthcheck offline
function_healthcheck_offline


# stop tomcat
function_stop_tomcat
[ "$?" -ne "0" ] && echo -e "$0 error: stop tomcat fail! \n" && exit 1

# check tomcat run states


# clean root && work
rm -rf "$p_root_path"/ROOT
rm -rf "$p_work_path"/*
[ "$?" -ne "0" ] && echo -e "$0 error: clean ROOT && work fail! \n" && exit 1
echo 'clean ROOT && work ok!'


# deploy war
unzip -q "$p_name".war -d "$p_root_path"/ROOT
[ "$?" -ne "0" ] && echo -e "$0 error: deploy war fail! \n" && exit 1
echo 'deploy war ok! '


# start tomcat
function_start_tomcat


# check effect
echo 'start check server effect...'
urlarr=$(echo $effect_url|awk '
{
    split($0, arr, ",")
}
END{
    for(i in arr) print arr[i]
}
')
for url in ${urlarr[@]};do
    url=`echo $url|sed 's/^[ \t]*//;s/[\t ]$//'`
    rs=`curl -G -s "$url"`
    if [ "$rs" = "ok" ];then
        echo check "$url" ok!
    else
        echo check "$url" fail! result: "$rs"
        exit 1
    fi
done
echo 'check server effect done! '


# healthcheck online
function_healthcheck_online


# delete deploy war
rm -f "$p_name".war

#################
# ext script
#################
# gzip log
if [ -f "$sh_gziplog" ];then
    [ ! -x "$sh_gziplog" ] && sudo chmod +x "$sh_gziplog"
    echo -e "\n"
    /bin/sh "$sh_gziplog"
else 
   echo 'WARN: not found gziplog script, skip it.'
fi



################
# log status
################
echo rebuild user: "`whoami`", rebuild date: `date +"%Y-%m-%d %H:%M:%S"` >> "$p_log_path"/rebuild.log


echo -e "\n$p_name rebuild work done! \n"
